﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using DevComponents.DotNetBar;
using BLL;
using Entities;

namespace WindowsApp.QLLopHoc
{
    public partial class frmAddLopHoc : DevComponents.DotNetBar.Office2007Form
    {
         private LopHocManager _manager;
        private LopHoc _LopHoc;
        private bool _isUpdate;
        frmQLLopHoc cha;
        public frmAddLopHoc()
        {
            InitializeComponent();
        }

        public frmAddLopHoc(LopHocManager _man,frmQLLopHoc p, LopHoc _lh=null) : this()
        {
            _manager = _man;
            _LopHoc = _lh;
            cha = p;
            _isUpdate = (_lh != null);
        }
        private void frmLopHoc_Load(object sender, EventArgs e)
        {
            if (_isUpdate)
            {
                txtMaLH.Text = _LopHoc.MaLop;
                mtbSiSo.Text = _LopHoc.SiSo.ToString();
                 
                 
            }
            txtMaLH.ReadOnly = _isUpdate;
        }

        private void btnSave_Click(object sender, EventArgs e)
        {
            if (!_isUpdate) _LopHoc = new LopHoc();

            _LopHoc.MaLop = txtMaLH.Text;
            _LopHoc.SiSo =Convert.ToInt32( mtbSiSo.Text);
            
            var vResult = _manager.Validate(_LopHoc);
            if (vResult.Valid)
            {
                var aResult = _manager.InsertOrUpdate(_LopHoc);
                if (aResult.Success)
                {
                    cha.btnLamMoi.PerformClick();
                    MessageBox.Show((_isUpdate?"Cập nhật":"Thêm")+"Thông tin lớp học thành công","Thông báo");
                    btnReset.PerformClick();
                }
                else
                    MessageBox.Show(aResult.GetErrorMessages());
            }
            else MessageBox.Show(vResult.ToString());
        }

        private void btnReset_Click(object sender, EventArgs e)
        {
            txtMaLH.Text = string.Empty;
            mtbSiSo.Text = "0";
                      
            this.Text = "Thêm lớp học";
            txtMaLH.ReadOnly = _isUpdate;
        }
    }
}
